package com.duguo.dynamicmvc.support.servlet;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.web.context.ContextLoader;

public class WebApplicationBasedServlet extends AbstractDynamicMVC implements Servlet{

	private ContextLoader _contextLoader;
	private ServletConfig _servletConfig;

	
	public void init(ServletConfig servletConfig) throws ServletException {
		_servletConfig=servletConfig;
		super.init(_servletConfig.getServletContext());
	}
	
	protected ListableBeanFactory createBeanFactory() {
		return getContextLoader().initWebApplicationContext(getServletContext());
	}


	protected void destroyBeanFactory() {		
		getContextLoader().closeWebApplicationContext(getServletContext());
	}


	public ServletConfig getServletConfig() {
		return _servletConfig;
	}


	public String getServletInfo() {
		return _servletConfig.getServletName();
	}

	public ContextLoader getContextLoader() {
		if(_contextLoader==null){
			_contextLoader =new ContextLoader();
		}
		return _contextLoader;
	}

	public void setContextLoader(ContextLoader contextLoader) {
		_contextLoader = contextLoader;
	}
	
	
	
}
